package com.hg.oa.module;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.hg.oa.R;
import com.hg.oa.common.BaseActivityWithToolBar;
import com.hg.oa.common.TabEntity;
import com.hg.oa.common.wifiupload.UploadTaskQueue;
import com.hg.oa.module.calendar.layer.DayReportFragment;
import com.hg.oa.module.home.layer.HomeFragment;
import com.hg.oa.module.home.presenter.HomePresenter;
import com.hg.oa.module.home.presenter.HomePresenterImpl;
import com.hg.oa.module.mine.layer.MineFragment;
import com.hg.oa.util.FileUtils;
import com.hg.oa.util.ViewFindUtils;

import java.util.ArrayList;

/**
 * Created by fengxiang on 2016/8/11.
 */
public class MainActivity extends BaseActivityWithToolBar {

    String tag = "MainActivity";
    HomePresenter presenter;
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    String[] mTitles = {"首页", "日历", "我的"};
    int[] mIconUnselectIds = {
            R.mipmap.tab_home_unselect, R.mipmap.tab_speech_unselect,
            R.mipmap.tab_contact_unselect};
    int[] mIconSelectIds = {
            R.mipmap.tab_home_select, R.mipmap.tab_speech_select,
            R.mipmap.tab_contact_select};
    ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    View mDecorView;
    CommonTabLayout mTabLayout_1;
    FragmentManager frgmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_tab);
        presenter = new HomePresenterImpl(this);
        frgmentManager = getSupportFragmentManager();
        mFragments.add(new HomeFragment());
        mFragments.add(new DayReportFragment());
        mFragments.add(new MineFragment());
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        mDecorView = getWindow().getDecorView();
        /** with nothing */
        mTabLayout_1 = ViewFindUtils.find(mDecorView, R.id.tl_1);
        mTabLayout_1.setTabData(mTabEntities);
        mTabLayout_1.setCurrentTab(0);
        presenter.switchPage(titleView, mTitles[0], R.id.fragments, frgmentManager.beginTransaction(), mFragments, mFragments.get(0));

        mTabLayout_1.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
//                if (position == 2) {
//                    toolbar.setVisibility(View.GONE);
//                }else{
//                    toolbar.setVisibility(View.VISIBLE);
//                }
                presenter.switchPage(titleView, mTitles[position], R.id.fragments, frgmentManager.beginTransaction(), mFragments, mFragments.get(position));
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.e(tag, "key back");
            FileUtils.createUploadTaskFile(this);
            FileUtils.wiriteUploadTaskQueueIntoFile(UploadTaskQueue.queue);
        }
        return super.onKeyDown(keyCode, event);
    }
}
